package com.itheima.xixi.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.xixi.Dto.DishDto;
import com.itheima.xixi.Dto.SetmealDto;
import com.itheima.xixi.Service.CategoryService;
import com.itheima.xixi.Service.SetmealService;
import com.itheima.xixi.Service.SetmeralDishService;
import com.itheima.xixi.common.R;
import com.itheima.xixi.entity.Category;
import com.itheima.xixi.entity.Dish;
import com.itheima.xixi.entity.Employee;
import com.itheima.xixi.entity.Setmeal;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    CategoryService categoryService;
    @Autowired
    SetmealService setmeralService;
    @Autowired
    SetmeralDishService setmeralDishService;

    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        //构造分页器
        Page<Setmeal> pageInfo = new Page(page,pageSize);
        Page<SetmealDto> dtoPage = new Page<>();
        //构造条件构造器
        LambdaQueryWrapper<Setmeal> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.like(name!=null, Setmeal::getName,name);
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        //执行查询
        setmeralService.page(pageInfo,queryWrapper);

        //对象拷贝
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");
        List<Setmeal> records = pageInfo.getRecords();
        List<SetmealDto> list = records.stream().map((item)->{
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item,setmealDto);
            //分类id
            Long categoryId = item.getCategoryId();
            //根据分类id查询分类对象
            Category category = categoryService.getById(categoryId);
            if (category!=null){
                //分类名称
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
            return setmealDto;
        }).collect(Collectors.toList());
        dtoPage.setRecords(list);
        return R.success(dtoPage);

    }
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        log.info("套餐信息：{}",setmealDto);
        setmeralService.saveWithDish(setmealDto);
        return R.success("新增套餐成功！！");
    }

    //删除套餐
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        R<String> r = setmeralService.removeWithid(ids);
        return r;
    }

    @PostMapping("/status/{st}")
    public R<String> status(@PathVariable Integer st,@RequestParam List<Long> ids){
        R<String> r = setmeralService.statusWithid(st, ids);
        return r;
    }
    //根据id查询套餐信息
    @GetMapping("/{id}")
    public R<SetmealDto> get(@PathVariable Long id) {

        SetmealDto setmeal = setmeralService.getByIdWithid(id);

        return R.success(setmeal);
    }
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){
        setmeralService.updateWithid(setmealDto);
        return R.success("套餐修改成功！！");
    }
    @GetMapping("/list")
    public R<List<Setmeal>> list (Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmeralService.list(queryWrapper);
        return R.success(list);
    }
}
